local jiyu = fk.CreateSkill{
  name = "hanqing__jiyu",
  tags = { Skill.Compulsory },
  dynamic_desc = function(self, player)
    return "hanqing__jiyu_inner:"..table.concat(table.map(player:getTableMark("@hanqing__jiyu"), function (s)
      return Fk:translate(string.sub(s, 2))
    end), " ")
  end,
}

Fk:loadTranslationTable{
  ["hanqing__jiyu"] = "急御",
  [":hanqing__jiyu"] = "锁定技，你使用非伤害牌或受到伤害时，需将下述前任意项移至〖回征〗描述中，并执行本次移除选项效果：②摸两张牌 ④伤害-1。",

  [":hanqing__jiyu_inner"] = "锁定技，你使用非伤害牌或受到伤害时，需将下述前任意项移至〖回征〗描述中，并执行本次移除选项效果：{1}。",

  ["#hanqing__jiyu-choice"] = "急御：移动前任意项并执行对应效果",
  ["@hanqing__jiyu"] = "急御",
  ["@pangjuan_draw2"] = "②",
  ["@pangjuan_damage_decrease"] = "④",
  ["pangjuan_draw2"] = "②摸两张牌",
  ["pangjuan_damage_decrease"] = "④伤害-1",

  ["$hanqing__jiyu1"] = "齐兵畏吾，故设此计也。",
  ["$hanqing__jiyu2"] = "不分早夜，兼程而进，日暮必至马陵。",
}

local function pangjuanMark(player, skill, result)
  local room = player.room
  local skill2 = skill == "hanqing__huizheng" and "hanqing__jiyu" or "hanqing__huizheng"
  for _, choice in ipairs(result) do
    room:removeTableMark(player, "@"..skill, "@"..choice)
    room:addTableMark(player, "@"..skill2, "@"..choice)
  end
  for _, s in ipairs({skill, skill2}) do
    if #player:getTableMark("@"..s) > 0 then
      local mark = player:getTableMark("@"..s)
      local new_mark = {}
      for _, choice in ipairs({"@pangjuan_damage_increase", "@pangjuan_draw2", "@pangjuan_discard2", "@pangjuan_damage_decrease"}) do
        if table.contains(mark, choice) then
          table.insert(new_mark, choice)
        end
      end
      room:setPlayerMark(player, "@"..s, new_mark)
    end
  end
end

local spec = {
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choices = table.map(player:getTableMark("@hanqing__jiyu"), function (choice)
      return string.sub(choice, 2)
    end)
    local result = room:askToCustomDialog(player, {
      skill_name = jiyu.name,
      qml_path = "packages/hanqing/qml/ProgressivelyChoicesBox.qml",
      extra_data = {
        choices, "#hanqing__jiyu-choice",
      }
    })
    result = (result ~= "") and json.decode(result) or {choices[1]}
    if #result == 0 then return end
    pangjuanMark(player, jiyu.name, result)
    player:broadcastSkillInvoke(jiyu.name)
    for _, choice in ipairs(result) do
      if choice == "pangjuan_damage_increase" then
        if event == fk.CardUsing then
          room:notifySkillInvoked(player, jiyu.name, "offensive")
          data.additionalDamage = (data.additionalDamage or 0) + 1
        elseif event == fk.DamageInflicted then
          room:notifySkillInvoked(player, jiyu.name, "negative")
          data:changeDamage(1)
        end
      elseif choice == "pangjuan_draw2" then
        room:notifySkillInvoked(player, jiyu.name, "drawcard")
        player:drawCards(2, jiyu.name)
        if player.dead then return end
      elseif choice == "pangjuan_discard2" then
        room:notifySkillInvoked(player, jiyu.name, "negative")
        room:askToDiscard(player, {
          min_num = 2,
          max_num = 2,
          include_equip = true,
          skill_name = jiyu.name,
          cancelable = false,
        })
        if player.dead then return end
      elseif choice == "pangjuan_damage_decrease" then
        if event == fk.CardUsing then
          room:notifySkillInvoked(player, jiyu.name, "negative")
          data.additionalDamage = (data.additionalDamage or 0) - 1
        elseif event == fk.DamageInflicted then
          room:notifySkillInvoked(player, jiyu.name, "defensive")
          data:changeDamage(-1)
        end
      end
    end
  end,
}

jiyu:addEffect(fk.CardUsing, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(jiyu.name) and player:hasSkill("hanqing__huizheng", true) and
      #player:getTableMark("@hanqing__jiyu") > 0 and not data.card.is_damage_card
  end,
  on_use = spec.on_use,
})

jiyu:addEffect(fk.DamageInflicted, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(jiyu.name) and player:hasSkill("hanqing__huizheng", true) and
      #player:getTableMark("@hanqing__jiyu") > 0
  end,
  on_use = spec.on_use,
})

jiyu:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, "@hanqing__jiyu", {"@pangjuan_draw2", "@pangjuan_damage_decrease"})
end)

jiyu:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@hanqing__jiyu", 0)
end)

return jiyu
